package cn.shyouchi.wuguanjia.android.ui.patrol.report.page;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.patrol.PatrolApi;
import cn.shyouchi.wuguanjia.android.base.BaseFragment;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.patrol.report.PatrolErrorAdapter;
import cn.shyouchi.wuguanjia.android.ui.patrol.report.PatrolErrorEntity;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class PatrolErrorFragment extends BaseFragment {

    @BindView(R.id.rv_patrol_error)
    RecyclerView mRvPatrolError;
    PatrolErrorAdapter mPatrolErrorAdapter;
    List<PatrolErrorEntity.ResultMessageBean> mPatrolErrorEntities = new ArrayList<>();

    PatrolApi mPatrolApi;

    public PatrolErrorFragment() {
    }


    @Override
    public void initInjector() {
        mPatrolApi = new PatrolApi(getContext());
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_patrol_error;
    }

    @Override
    public void getBundle(Bundle bundle) {

    }

    @Override
    public void initUI(View view) {
        ButterKnife.bind(this, view);
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
//        mPatrolErrorEntities.add(new PatrolErrorEntity());
        mPatrolErrorAdapter = new PatrolErrorAdapter(mPatrolErrorEntities);
        mRvPatrolError.setAdapter(mPatrolErrorAdapter);
        mRvPatrolError.setLayoutManager(new LinearLayoutManager(getContext()));

        showContent(false);


    }

    @Override
    public void initData() {
        fetchPatrolErrors();
    }


    private void fetchPatrolErrors() {
        mPatrolApi.getWarrantyList("1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<PatrolErrorEntity>(getContext()) {
                    @Override
                    public void onNext(PatrolErrorEntity entity) {
                        mPatrolErrorEntities.clear();
                        mPatrolErrorEntities.addAll(entity.getResultMessage());
                        mPatrolErrorAdapter.notifyDataSetChanged();
                    }
                });

    }
}
